(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.role.updateContinueController', updateContinueController);
	updateContinueController.$inject = ['$q', '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'roleService' ];
	function updateContinueController($q, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, roleService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.role = {};
		
		ctrl.init = function() {
			ctrl.roleRightGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.roleRight.tableConfigs.table, rowSelectionChanged);
			ctrl.roleRightGridOptions.data = 'vm.role.roleRights';
			ctrl.roleRightGridOptions.verifyData = 'vm.roleOld.roleRights';
			
			authService.updateContinue(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					// init role data
					ctrl.role = response.data.model || params;
					ctrl.role.taskId = response.data.task.taskId;
				}
			});
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedDTParmList = selectedRows;
		}
		
		ctrl.addRoleRight = function(row) {
			return $q(function(resolved, rejected){
				var filterRightCodes = [];
				ctrl.role.roleRights = ctrl.role.roleRights || [];
				for (var i = 0; i < ctrl.role.roleRights.length; i++) {
					filterRightCodes.push(ctrl.role.roleRights[i].rightCode);
				}
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.right',
					'params':{ multiSelect : true, "filterRightCodes" : filterRightCodes},
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								return false;
							}
							if (!ctrl.role.roleRights) {
								ctrl.role.roleRights = [];
							}
							angular.forEach( selectedRows, function (selectedOne, key) {
								selectedOne.dataStatus = 'C';
								ctrl.role.roleRights.push(selectedOne);
							});
						},
						'close' : function(){
							rejected();
						}
					}
				});
			});
		}
		
		ctrl.updateContinueSave = function() {
			return authService.updateContinueSave(ctrl.role).then(saveSuccessful, saveFailed);
		};

		ctrl.updateContinueSubmit = function() {
			return authService.updateContinueSubmit(ctrl.role).then(submitSuccessful, submitFailed);
		};

		ctrl.updateContinueCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.role.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.updateContinueCancel(ctrl.role).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.role.list',
					'state' : 'cda.role.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.role, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.role.list',
					'state' : 'cda.role.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);